package com.ag.lzm.dao;

import java.sql.SQLException;
import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.springframework.orm.hibernate3.HibernateCallback;

import com.ag.bean.TbIds;
import com.ag.common.IDUtil;

public class TbPlanDAO extends com.ag.dao.TbPlanDAO {
	private static final Log log = LogFactory.getLog(TbPlanDAO.class);

	public TbIds findPlanPK() {
		log.debug("finding TbPlan primary key");
		try {
			String queryString = "from TbIds ids where ids.idsId = "
					+ IDUtil.TB_PLAN;
			return (TbIds) getHibernateTemplate().find(queryString).get(0);
		} catch (RuntimeException re) {
			log.error("find primary key failed", re);
			throw re;
		}
	}

	public List find(String queryString) {
		log.debug("finding all instances");
		try {
			return getHibernateTemplate().find(queryString);
		} catch (RuntimeException re) {
			log.error("find failed", re);
			throw re;
		}
	}

	public List find(String queryString, Object value) {
		log.debug("finding all instances with option: " + value);
		try {
			return getHibernateTemplate().find(queryString, value);
		} catch (RuntimeException re) {
			log.error("find all instances with option failed", re);
			throw re;
		}
	}

	/**
	 * 分页查询方法
	 * 
	 * @param hsql
	 * @param firstRow
	 * @param maxRow
	 * @return
	 * @throws Exception
	 */
	public List find(final String hsql, final int firstRow, final int maxRow)
			throws Exception {
		return getHibernateTemplate().executeFind(new HibernateCallback() {
			public Object doInHibernate(Session s) throws HibernateException,
					SQLException {
				Query query = s.createQuery(hsql);
				query.setFirstResult(firstRow);
				query.setMaxResults(maxRow);
				List list = query.list();
				return list;
			}
		});
	}
}
